<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/xadmin/css/font.css}">
    <link rel="stylesheet" th:href="@{/xadmin/css/xadmin.css}">
    <script th:src="@{/xadmin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/xadmin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-body {
            overflow-x: hidden;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">分数查看</a>
                <a>
                    <cite>宿舍分数</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <select name="termId" lay-verify="required" class="show-term-container"></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="set">设置</button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table" id="test" lay-filter="test"></table>
                    <!--<table class="layui-table"
                           lay-data="{url:'/apartment/score/stu',page:false,toolbar: '#toolbarDemo',id:'test'}"
                           lay-filter="test">
                        <thead>
                        <tr>
                            <th lay-data="{field:'id', width:80, sort: true}">ID</th>
                            <th lay-data="{field:'score', minWidth: 120}">分数</th>
                            <th lay-data="{field:'remark', minWidth: 120}">备注</th>
                            <th lay-data="{field:'createTime', minWidth:180, templet:'<div>{{layui.util.toDateString(d.createTime*1000, `yyyy-MM-dd HH:mm:ss`)}}</div>'}">
                                提交时间
                            </th>
                        </tr>
                        </thead>
                    </table>-->
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var load = layer.load(1);
        getTerm(function () {
            layui.form.render('select');
            getTable();
            layer.close(load);
        });
        form.on('submit(set)', function (data) {
            console.log(data.field);
            let termId = data.field['termId'];
            sessionStorage.setItem('termId', termId);
            table.reload('test', {
                url: '/apartment/score/stu?termId=' + termId
            });
            // console.log("重新渲染完成termId " + sessionStorage.getItem('termId'));
            return false;
        });

        function getTerm(callback) {
            $.ajax({
                url: "/term",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = '';
                    if (result.data && result.data.length > 0) {
                        sessionStorage.setItem("termId", result.data[0].id);
                        let termId = result.data[0].id;
                        console.log(termId);
                        for (var i in result.data) {
                            console.log(result.data[i].id);
                            if (result.data[i].id == termId) {
                                console.log("==");
                                html += '<option value="' + result.data[i].id + '" selected="selected">' + result.data[i].name + '</option>';
                            } else
                                html += '<option value="' + result.data[i].id + '">' + result.data[i].name + '</option>';
                        }
                        $(".show-term-container").html(html);
                    }
                    if (callback) {
                        callback();
                    }
                    // console.log(html)
                }
            });
        }

        function getTable() {
            table.render({
                elem: '#test',
                url: '/apartment/score/stu?termId=' + sessionStorage.getItem('termId'),
                toolbar: '#toolbarDemo',
                cols: [
                    [
                        {type: 'numbers', width: 80, sort: true, title: '序号'},
                        {field: 'scoreStr', minWidth: 120, title: '分数'},
                        {field: 'remark', minWidth: 120, title: '备注'},
                        {
                            field: 'createTime',
                            minWidth: 180,
                            templet: '<div>{{layui.util.toDateString(d.createTime*1, `yyyy-MM-dd HH:mm:ss`)}}</div>',
                            title: '提交时间'
                        },
                        {field: 'week', minWidth: 90, title: '周数'}
                    ]
                ]
            });
        }
    });
</script>
</html>